﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Management.Entity;
namespace Management.ViewModel
{
    public class EasyUITree
    {
        public int id { get; set; }
        public string text { get; set; }
        public string state { get; set; }
        //public bool Checked {get;set;}
        public string attributes { get; set; }
        public List<EasyUITree> children { get; set; }
        /// <summary>
        /// 菜单
        /// </summary>
        /// <param name="modulelist"></param>
        /// <returns></returns>
        public static List<EasyUITree> Totree(List<Module> modulelist)
        {
            List<EasyUITree> list=new List<EasyUITree> ();
            var pearentmunu = modulelist.Where(q => q.ParentID == 0);
            foreach (var item in pearentmunu)
            {
                EasyUITree tree = new EasyUITree();
                tree.id = item.MID;
                tree.text = item.ModuleName;
                tree.state = "closed";
                //tree.Checked = false;
                tree.attributes = item.ModuleLinkUrl;
                tree.children = AddChildren(item.MID, modulelist);
                list.Add(tree);
            }
            return list;
        }
        /// <summary>
        /// 加载子菜单
        /// </summary>
        /// <param name="p">父菜单id</param>
        /// <param name="modulelist">菜单集</param>
        /// <returns></returns>
        private static List<EasyUITree> AddChildren(int p, List<Module> modulelist)
        {
            List<EasyUITree> list = new List<EasyUITree>();
            var itemlist = modulelist.Where(q => q.ParentID == p);
            foreach (var item in itemlist)
            {
                EasyUITree tree = new EasyUITree();
                tree.id = item.MID;
                tree.text = item.ModuleName;
                tree.state = "closed";
                //tree.Checked = false;
                tree.attributes = item.ModuleLinkUrl;
                list.Add(tree);
            }
            return list;
        }
        
    }
}
